<template>
	<view class="pagesWhite">
		<view class="header_bar">
			<view class="header">
				<view class="header-goBack" @tap.stop="$tools.back(1)"></view>
				<view class="header-title">修改登录密码</view>
			</view> 
		</view> 
		<view class="operateBox-tabs">
			<!-- <view class="operateBox-formBox">
				<view class="operateBox-formBox-list-header">
					<image class="operateBox-formBox-list-icon" src="../../static/operate/phone.png" mode=""></image>
				</view>
				<view class="operateBox-formBox-list operateBox-formBox-listBorder" :class="{ 'operateBox-formBox-list-input--active': isFocus == 0, showCloseBtn: phone != '' }">
					<input type="text" placeholder="请输入手机号码" :value="userInfo.regAddress | conceal(4)" placeholder-class="inputStyle"
					disabled="disabled"
					@blur="blurInput(0)"
					@focus="focusInput(0)"/>
				</view>
			</view> -->
			<view class="operateBox-flex">
				<view class="operateBox-formBox-list-header">
						<image class="operateBox-formBox-list-icon" src="../../static/operate/password.png" mode=""></image>
					</view>
				<view class="operateBox-formBox-list" :class="{ 'operateBox-formBox-list-input--active': isFocus == 3, showCloseBtn: new_password != '' }">
					<input placeholder="请输入新的登录密码" placeholder-class="inputStyle" v-model="new_password"
					:password="!eyeShowHideArray[3].show"
					@blur="blurInput(3)"
					@focus="focusInput(3)"/>
					<view class="operateBox-formBox-list-image">
						<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('new_password')">
							<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
						</view>
						<view class="operateBox-formBox-list-image__see" @click="toggleEyeStatus(3)">
							<image :src="eyeShowHideArray[3].show ? eyeShow : eyeHide" mode="widthFix"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="operateBox-flex">
				<view class="operateBox-formBox-list-header">
						<image class="operateBox-formBox-list-icon" src="../../static/operate/password.png" mode=""></image>
					</view>
				<view class="operateBox-formBox-list" :class="{ 'operateBox-formBox-list-input--active': isFocus == 4, showCloseBtn: re_password != '' }">
					<input placeholder="请再次确认密码" placeholder-class="inputStyle" v-model="re_password"
					:password="!eyeShowHideArray[4].show"
					@blur="blurInput(4)"
					@focus="focusInput(4)"/>
					<view class="operateBox-formBox-list-image">
						<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('re_password')">
							<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
						</view>
						<view class="operateBox-formBox-list-image__see" @click="toggleEyeStatus(4)">
							<image :src="eyeShowHideArray[4].show ? eyeShow : eyeHide" mode="widthFix"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="operateBox-flex">
				<view class="operateBox-formBox-list-header">
					<image class="operateBox-formBox-list-icon" src="../../static/operate/icon-code.png" mode=""></image>
				</view>
				<view class="operateBox-formBox-list" :class="{ 'operateBox-formBox-list-input--active': isFocus == 1, showCloseBtn: code != '' }">
					<input type="number" placeholder="请输入验证码" v-model="code" placeholder-class="inputStyle"
					@blur="blurInput(1)"
					@focus="focusInput(1)"/>
					<view class="operateBox-formBox-list-image">
						<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('code')">
							<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
						</view>
						<view class="operateBox-formBox-list-image__code" @click="sendCode()">
							<text class="code">{{ remain }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="initBtn" @tap.stop="$u.throttle(confirm,1000)">确定</view>
	</view>
</template>

<script>
	import md5 from '../../common/js/md5.js';
	export default {
		data() {
			return {
				phone: '',
				code: '',
				password: '',
				new_password: '',
				re_password: '',
				// 验证码
				remain: '获取验证码',
				isSend: true,
				time: 60,
				timer: null,
				isFocus: null,
				eyeShow: '../../static/public/icon-eye-open.png',
				eyeHide: '../../static/public/icon-eye-close.png',
				eyeShowHideArray: [{ show: false }, { show: false }, { show: false }, { show: false }, { show: false }],
				userInfo: {},
			}
		},
		onShow() {
			this.init();
		},
		methods: {
			init(){
				this.getUserInfo();
			},
			confirm(){
				// if (this.phone == '' ) {
				// 	return this.$tools.toast('请输入手机号码  ');
				// }
				// var reg = /^1[3456789]\d{9}$/;
				// if(!reg.test(this.phone)){
				// 	return this.$tools.toast('请输入正确的手机号码！！！');
				// }
				if (this.code == ''){
					return this.$tools.toast('请输入验证码');
				}
				if (this.new_password == ''){
					return this.$tools.toast('请输入新密码');
				}
				var reg = /^(?![0-7]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
				if(!reg.test(this.new_password)){
					return this.$tools.toast('请输入6-12位字母+数字的登录密码');
				}
				if (this.re_password == ''){
					return this.$tools.toast('请再次输入新密码');
				}
				if (this.new_password != this.re_password){
					return this.$tools.toast('两者密码不一致');
				}
				var data = {};
				data['twoPwd'] =  this.$md5(this.re_password);
				data['newPwd'] =  this.$md5(this.new_password);
				data['type'] =  1;
				data['code'] =  this.code;
				data['codeType'] = 1;
				console.log("data：",data);
				this.$Ajax('/api/user/updatePwd',
				data,
				res => {
						let that = this;
						// uni.clearStorage();    //  清除本地所以缓存
						uni.removeStorageSync('token');
						uni.removeStorageSync('user');
						uni.removeStorageSync('address_id');
						that.$tools.toast('修改登录密码成功');
						setTimeout(function() {
							that.$tools.reLaunch('/pages/operate/login');
						}, 1000)
					},err => {
					}
				)
			},
			sendCode() {
				if(this.isSend == true){
					uni.showLoading({
						title: '发送中',
						mask: true
					});
					var data = {};
					this.phone = this.userInfo.regAddress;
					data['account'] = this.phone;
					data['type'] = 5;
					console.log("data：",data);
					this.$Ajax('/api/user/sendCode',
					data,
					res => {
							this.isSend = false;
							this.remain = this.time + 's';
							this.timer = setInterval(() => {
								this.time--;
								this.remain = this.time + 's';
								if (this.time == 0) {
									clearInterval(this.timer);
									this.remain = '重新获取';
									(this.isSend = true), (this.time = 60);
								}
							}, 1000);
						}
					)
					uni.hideLoading();
				}
			},
			/* clear & eye */
			focusInput(index) {
				this.isFocus = index;
			},
			blurInput(index) {
				this.isFocus = null;
			},
			clearData(keyName) {
				for (var key in this.$data) {
					if (key == keyName) {
						this.$data[key] = '';
					}
				}
			},
			toggleEyeStatus(index) {
				this.eyeShowHideArray[index].show = !this.eyeShowHideArray[index].show;
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/common/scss/pages/operate/register';
	.initBtn{
		margin: 68rpx 34rpx 0;
		width: calc(100% - 68rpx);
	}
	.operateBox-tabs{
		margin: 64rpx 50rpx;
	}
</style>
